package Bank;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Manager {
		
	private int customer_id;
	private String customer_firstname;
	private String customer_lastname;
	private String customer_username;
	private String customer_password;
	private long customer_phonenumber;
	private String customer_address;
		
	public void registerCustomer(){
		Scanner in = new Scanner(System.in);
		String phone,fname,uname,lname;
		long id=0;
        boolean flag_id;
		System.out.println("Please enter Customer's information:-");
	do{	
		flag_id = true;
		System.out.print("customer_id:-");
		
	try{
		id = in.nextLong();
		in.nextLine();
		String query_c = "select * from customer_table where customer_id = "+id;
		ConnDB cdb1 = new ConnDB();
		int n_c= cdb1.uprs(query_c);
		String query_m = "select * from employee_table where employee_id = "+id;
		ConnDB cdb2 = new ConnDB();
		int n_m = cdb2.uprs(query_m);
		if(n_c!=0){
			System.out.println("The customer ID is existed in customer! Please enter again~");
			flag_id = false;
		}
		if(n_m!=0){
			System.out.println("The customer ID is used in employee! Please enter again~");
			flag_id = false;
		}
       }catch(InputMismatchException e){
    	   in.nextLine();
    	   System.out.println("The id only for digits!!");
    	    flag_id = false;
	    }	
	 }while(flag_id == false);
	
	  //first name must be identifiers
		boolean flag_fname;
	do{
		flag_fname=true;
	    System.out.print("customer_firstname(6-20 words):-");
		fname = in.nextLine();
		String fname_z = "\\w{6,20}";
		if(!fname.matches(fname_z)){
			System.out.println("The first name must be identifiers ! Please enter again~");
			flag_fname=false;
		}
	   }while(flag_fname==false);
		
	    //last name must be identifiers
	     boolean flag_lname;
	do{
		flag_lname = true;
		System.out.print("customer_lastname(6-20 words):-");
		lname = in.nextLine();
		String lname_z = "\\w{6,20}";
		if(!lname.matches(lname_z)){
			System.out.println("The last name must be digits or letters! Please enter again~");
			flag_lname=false;
		}
	   }while(flag_lname==false);
		
	//user_name must beidentifiers
	   boolean flag_uname;
	do{
		flag_uname = true;
		System.out.print("customer_username(6-20 words):-");
		uname = in.nextLine();
		String uname_z = "\\w{6,20}";
		if(!uname.matches(uname_z)){
			System.out.println("The user name must be digits or letters! Please enter again~");
			flag_uname=false;
		}
	   }while(flag_uname==false);
	
	//Employee_password(only for six digits)
		boolean flag1=false;
		String password;
		do{
			System.out.print("Employee_password(It's only for six digits):-");
		    String password1 = "\\d{6}";
		    password = in.nextLine();
		    if(password.matches(password1)){		   
		        flag1 = true;			
		    }else{
			    System.out.println("The password is illegal!Please enter again~");
		       }
		    
		  }while(flag1==false);	
		
		System.out.print("customer_phonenumber:-");
		boolean flag_phone;
		do{
			flag_phone = false;
			String phone3 = "\\d{11}";
			String phone2 = "\\d{14}";
			phone = in.nextLine();
			if(phone.indexOf("+")==0){
			  if(phone.length()==14){
					String lphone = phone.substring(1);
					if(lphone.matches(phone2)){
						flag_phone = true;
					}else{
						System.out.println("It's only for digits!");
					}
				}else{
					System.out.println("There must be 14 digits after "+"+!");
					System.out.print("Please enter again:-");
				}
			}else{
				if(phone.matches(phone3)){
					flag_phone = true;
				}else{
					System.out.println("There must be 11 digits!");
					System.out.print("Please enter again:-");
				}
			}		
		
		}while(flag_phone==false);
		System.out.print("customer_address:-");
		String address = in.nextLine();				
		String Query="insert into CUSTOMER_TABLE values("+id+",'"+fname+"','"+lname+"','"+uname+"','"+password+"','"+phone+"','"+address+"')";	
		int flag = new ConnDB().uprs(Query);
		if(flag==1){
			System.out.println("RegisterCustomer sucessfully!");
		
		}else{
			System.out.println("RegisterCustomer failed!");
		}	
	}
	
	public void deleteCustomer(){
		Scanner in = new Scanner(System.in);
		String username;
		
		//username must be identifiers
		boolean flag_uname;
	do{
		flag_uname = true;
		System.out.println("please input username");		
		username = in.nextLine();
		String uname_z = "\\p{Alnum}";
		if(!username.matches(uname_z)){
			System.out.println("The username must be digits or letters! Please enter again~");
			flag_uname=false;
		}
	   }while(flag_uname==false);
	
		if(new ConnDB().usernameIsExist("customer_table",username) == 1){
			System.out.println("exist");
			String str = "delete from customer_table where CUSTOMER_USERNAME ='"+username +"'";
			System.out.println(str);
			int i = new ConnDB().uprs(str);
			if(i==1){
				System.out.println("delete successed");
			}else{
				System.out.println("delete failed");
			}
		}else{
			System.out.println("username is not exist");
			deleteCustomer();
		}
	}
	
	
	
	public void registerAccount(){
		Scanner in = new Scanner(System.in);
		long id;
		String accountid;
		String query_cid;
		boolean flag_custexist;
		try{
	do{
		flag_custexist=false;
		System.out.print("Enter the customer's ID:-");		
		
		id = in.nextLong();		
		in.nextLine();
		query_cid = "select customer_id from customer_table where customer_id = "+id;
		int n_cexist = new ConnDB().uprs(query_cid);
		if(n_cexist==0){
			System.out.println("The customer is not exist,Please enter again!");
			flag_custexist=true;
		}
		}while(flag_custexist==true);
		
		ResultSet rs = new ConnDB().getrs(query_cid);
		try {
			while(rs.next()){
				if(rs.getInt(1)==id){
					boolean flag1;
					do{   
						flag1 = false;
						System.out.print("Enter account's ID:-");
						accountid = in.nextLine();
						int flag_id = new ConnDB().idIsExist("account_table",Long.valueOf(accountid));
						if(flag_id==1){
							System.out.println("The accountId has been exsited!!Please Enter again!");
							flag1 =true;
						}
					}while(flag1 == true);
					
					Date nowDate = new Date();
					String form = String.format("%tF", nowDate);
					String ctype = null;
					boolean flag;
					do {
						flag = true;
						System.out.println("Choice type : 1.Savings  2.Current");					
						System.out.print("Enter customer's Account_type:-");
						String choice = in.nextLine();
						if (choice.equals("1")) {
							ctype = "Savings";
						} else if (choice.equals("2")) {
							ctype = "Current";
						} else {
							System.out.println("Wrong chioce");
							flag = false;
						}
					} while (flag == false);
					
					String Query="insert into account_table values("+accountid+","+id+",'"+ctype+"',"+0+","+"TO_DATE('"+form+"','YYYY-MM-DD'))";
					int n = new ConnDB().uprs(Query);
					if(n==1){
						System.out.println("RegisterAccount sucessfully!");
					
					}else{
						System.out.println("RegisterAccount failed!");
					}
				}
				    					
				}			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}catch(InputMismatchException e){
			System.out.println("Only for inputing digits,please enter again");
			in.nextLine();
			registerAccount();
		 }finally{
			 new ConnDB().close();
		 }
	}
	
	public void viewCustomer() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the customer's Id");
		try{
		long id = in.nextLong();
		in.nextLine();
		String Query = "select * from customer_table where customer_id = "+id;
		ConnDB db1 = new ConnDB();
		ResultSet rs = db1.getrs(Query);
		try {
			if(rs.next()){
				System.out.println("Id\tFtname\tLname\tUname\tPword\tphonenumber\taddress");
				System.out.println(rs.getLong(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getString(5)+"\t"+rs.getString(6)+"\t"+rs.getString(7));
			}else{
				System.out.println("The customer is not exsit!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db1.close();
		}
		}catch(InputMismatchException e){
			System.out.println("Only for inputing digits,please enter again");
			in.nextLine();
			viewCustomer();
		}
	}
	
	public void viewAccount(){
		Scanner in = new Scanner(System.in);
		long array[] = new long[10];
		boolean flag_cexist;
		String Q_account;
		ResultSet rs_choice=null;
		int n ;
		try{
	do{
		flag_cexist = true;
		System.out.print("Enter the customer's Id:-");
		
		long cust_id = in.nextLong();
		in.nextLine();
		String Query = "select * from customer_table where customer_id = "+cust_id;
		ConnDB db2 = new ConnDB();
		ResultSet rs = db2.getrs(Query);
	    Q_account = "select * from account_table where customer_id = "+cust_id;
		n =  new ConnDB().uprs(Q_account);
		
		try {
			if(rs.next()){				
				ResultSet rs_account = new ConnDB().getrs(Q_account);				
				
				if(!rs_account.next()){
					System.out.println("The account_id is not exist,you can regist account first!");				
				}
				
			}else{
				System.out.println("The customer is not exist!Please enter again~");
				flag_cexist = false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}while(flag_cexist == false);
						
			int choice;
			boolean flag_choice;
			do{
				ResultSet rs_account1 = new ConnDB().getrs(Q_account);
				int j = 0,i=1;
				try {
					System.out.println("******************");
					System.out.println("***  no. Account_id   ***");
					while(rs_account1.next()){		
						array[j] = rs_account1.getLong("account_id");						
						System.out.println("***  "+i++ +".\t  "+rs_account1.getLong("account_id")+"   ***");
						j++;
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}	
				System.out.println("***  "+i++ +".\t  " +"Exist   ***");
				System.out.println("******************");
			flag_choice = true;
			System.out.print("Enter the choice:-");
			choice = in.nextInt();
			if(choice==n+1){
				break;
			}
			if(choice<1||choice>n+1){
				flag_choice = false;
				System.out.println("Enter wrong,Please enter again!");
			}else{
				flag_choice = false;
				long id_choice = array[choice-1];
				String Query1 = "select * from account_table where account_id = "+id_choice;
				rs_choice = new ConnDB().getrs(Query1);
				try {
					if(rs_choice.next()){
						System.out.println("ac_Id\tcust_Id\ttype\tBalance\tDate");
						System.out.println(rs_choice.getLong(1)+"\t"+rs_choice.getLong(2)+"\t"+rs_choice.getString(3)+"\t"+rs_choice.getFloat(4)+"\t"+rs_choice.getDate(5));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			}while(flag_choice == false);
			
      	
		}catch(InputMismatchException e){
			System.out.println("Only for inputing digits,please enter again");
			in.nextLine();
			viewAccount();
		}finally{
			new ConnDB().close();
		}
	}
	
	public void customerDetails(){
		Scanner in = new Scanner(System.in);
		System.out.print("Enter the customer's ID that you want to update:-");
		try{
		long cust_id = in.nextLong();
		in.nextLine();
		String Query = "select * from customer_table where customer_id = "+cust_id;
		ResultSet rs = new ConnDB().getrs(Query);
		try {
			System.out.println("****************************");
			if(rs.next())
			{
				System.out.println("***   1.Name:-"+rs.getString(4)+"   ***\n"+"***   2.Phone:-"+rs.getLong(6)+" *\n"+"***   3.Address:-"+rs.getString(7)+"   ***\n***   4.exit          ***");
			}
			else{
				System.out.println("The ID is not exsit,Please enter again~");
				customerDetails();
			}
			System.out.println("****************************");
			String select;
			do{
		    System.out.print("Select:-");
			select = in.nextLine();
			switch(select)
			{
				case"1":{
					String newname;
					boolean Flag_fnewname;
					do{
					Flag_fnewname = true;
					String fnnname_z = "\\w{6,20}";
					System.out.print("Enter your new username(6-20 words):-");
					newname = in.nextLine();
					if(!newname.matches(fnnname_z)){
						Flag_fnewname=false;
					}
					}while(Flag_fnewname==false);
					
					String query = "update customer_table set CUSTOMER_USERNAME = '"+newname+"' where customer_id = "+cust_id;
					int n = new ConnDB().uprs(query);
					if(n==1){
						System.out.println("Update customer name sucessfully!");
					}
					ResultSet rs1 = new ConnDB().getrs(Query);
					if(rs1.next()){
						System.out.println("****************************");
					    System.out.println("***   1.Name:-"+rs1.getString(4)+"   ***\n"+"***   2.Phone:-"+rs1.getLong(6)+" *\n"+"***   3.Address:-"+rs1.getString(7)+"   ***\n***   4.exit");					
					    System.out.println("****************************");
					}break;
				}				
				case"2":{
					System.out.print("Enter your new phonenumber:-");					
					boolean flag_phone;
					String  phone = null;
					do{
						flag_phone = false;
						String phone3 = "\\d{11}";
						String phone2 = "\\d{14}";
						phone = in.nextLine();
						if(phone.indexOf("+")==0){
						  if(phone.length()==14){
								String lphone = phone.substring(1);
								if(lphone.matches(phone2)){
									flag_phone = true;
								}else{
									System.out.println("It's only for digits!");
								}
							}else{
								System.out.println("There must be 14 digits after "+"+!");
								System.out.print("Please enter again:-");
							}
						}else{
							if(phone.matches(phone3)){
								flag_phone = true;
							}else{
								System.out.println("There must be 11 digits!");
								System.out.print("Please enter again:-");
							}
						}
					}while(flag_phone==false);	
					String query = "update customer_table set CUSTOMER_PHONENUMBER ='"+Long.parseLong(phone)+"' where customer_id = "+cust_id;
					int n = new ConnDB().uprs(query);
					if(n==1){
						System.out.println("Update customer phonenumber sucessfully!");
					}
					ResultSet rs2 = new ConnDB().getrs(Query);
					if(rs2.next()){
						System.out.println("****************************");
					    System.out.println("***   1.Name:-"+rs2.getString(4)+"   ***\n"+"***   2.Phone:-"+rs2.getLong(6)+" *\n"+"***   3.Address:-"+rs2.getString(7)+"   ***\n***   4.exit");					
					    System.out.println("****************************");
					}
					break;				
				}
				case"3":{
					System.out.print("Enter your new address:-");
					String address = in.nextLine();
					String query = "update customer_table set CUSTOMER_ADDRESS ='"+address+"' where customer_id = "+cust_id;
					int n = new ConnDB().uprs(query);
					if(n==1){
						System.out.println("Update customer address sucessfully!");
					}
					ResultSet rs3 = new ConnDB().getrs(Query);
					if(rs3.next())
					{
						System.out.println("****************************");
					    System.out.println("***   1.Name:-"+rs3.getString(4)+"   ***\n"+"***   2.Phone:-"+rs3.getLong(6)+" *\n"+"***   3.Address:-"+rs3.getString(7)+"   ***\n***   4.exit");					
					    System.out.println("****************************");
					}
					break;
				}
				case"4":{
					System.out.println("Thank you !");
					break;
				}
				default:{
					System.out.println("Wrong choice!!");
					break;
				}
			}
			}while(!select.equals("4"));
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		}catch(InputMismatchException e){
			System.out.println("Only for inputing digits,please enter again");
			in.nextLine();
			customerDetails();
		}finally{
			new ConnDB().close();
		}
	}
	
	public void accountDetails() { 
		Scanner in = new Scanner(System.in);
		boolean flag_aexist;
		String Q_account;
		long account_id;
		try{
		do{
			flag_aexist=true;
		    System.out.print("Enter the account's ID that you want to update:-");
		
			account_id = in.nextLong();
			in.nextLine();
			Q_account = "select * from account_table where account_id = "+account_id;
			int n_aexist = new ConnDB().uprs(Q_account);
			if(n_aexist==0){
				System.out.println("The account is not exist! Please enter again~");
				flag_aexist=false;
			}
		   }while(flag_aexist==false);
		        
			
				String select;
				do{
					ResultSet rs = new ConnDB().getrs(Q_account);
			        System.out.println("****************************");
			        while(rs.next()){
				       System.out.println("1.Account type:-"+rs.getString(3)+"\n2.DateofOpening:-"+rs.getDate(5)+"\n3.exit \n");
		           	}
			        System.out.println("****************************");
					
				System.out.print("Enter your Choice:-");
				select = in.nextLine();
				switch(select){								
				case "1":{
					ResultSet rs1 = new ConnDB().getrs(Q_account);
					if(rs1.next())
					System.out.println("The old type:-"+rs1.getString(3));
					String ctype = null;
					boolean flag;
					do {
						flag = true;
						 System.out.println("**************************************");
						System.out.println("* Choice type : 1.Savings  2.Current *");						
						 System.out.println("**************************************");
						System.out.print("Choice:-");
						String choice = in.nextLine();
						if (choice.equals("1")) {
							ctype = "Savings";
						} else if (choice.equals("2")) {
							ctype = "Current";
						} else {
							System.out.println("Wrong chioce");
							flag = false;
						}
					} while (flag == false);
					String query = "update account_table set ACCOUNT_TYPE ='"+ctype+"' where account_id = "+account_id;
					int n = new ConnDB().uprs(query);
					if(n==1){
						System.out.println("Update account's type sucessfully!");
					}
					break;
				}
				case "2":{
					Date nowDate = new Date();
					String form = String.format("%tF", nowDate);
					String Query="update account_table set ACCOUNT_DATEOFOPENING ="+"TO_DATE('"+form+"','YYYY-MM-DD')"+" where account_id = "+account_id;
					int n = new ConnDB().uprs(Query);
					ResultSet rs_date = new ConnDB().getrs(Q_account);
					while(rs_date.next()){
						System.out.println("Update Date:-"+rs_date.getDate(5));
					}
					if(n==1){
						System.out.println("Update account's DateofOpening sucessfully!");
					}	
					
					break;
				}
				case "3":{
					System.out.println("Thank you !");
					break;
				}default:{
					System.out.println("Wrong choice!!Enter again~");
					break;
				}				
				}
				}while(!select.equals("3"));
			} catch (SQLException e) {
				e.printStackTrace();
			}catch(InputMismatchException e){
				System.out.println("Only for inputing digits,please enter again");
				in.nextLine();
				accountDetails();
			}finally{
				new ConnDB().close();
			}
		
	}

}
